package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chenenyu.router.annotation.Route;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.SubwayBean;
import sz.szsmk.citizencard.model.network.bean.response.SubwayLineBean;
import sz.szsmk.citizencard.ui.adapter.SubwaySitelistAdapter;
import sz.szsmk.citizencard.ui.adapter.SubwaySelectLineAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.CommonUtils;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.MesageEventBus;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/7 10:43
 * description:地铁 选择起点 二级菜单
 */
@Route("SubwayStartActivity")
public class SubwayStartActivity extends IBaseActivity  {
    public static SubwayStartActivity instance;

    ImageView back;
    TextView settingTitle;
    TextView bjTv;
    ImageView shareIv;
    RecyclerView subwayLineLv;
    RecyclerView subwaySiteLv;
    private SubwaySelectLineAdapter subwaySelectLineAdapter;

    private List<String> subwayLine;

    private SubwaySitelistAdapter sitelistAdapter;
    private SubwayLineBean subwayLineBean;
    private String type;

    List<SubwayLineBean.LineListBean.GBean> data ;
    private int currentLine;

    @Override
    protected int attachLayoutRes() {
        return R.layout.subway_start_activity;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        instance = this;

        back = findViewById(R.id.back);
        settingTitle = findViewById(R.id.setting_title);

        bjTv = findViewById(R.id.bj_tv);

        shareIv = findViewById(R.id.share_iv);

        subwayLineLv = findViewById(R.id.subway_line_lv);

        subwaySiteLv = findViewById(R.id.subway_site_lv);


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        settingTitle.setText("选择起点");
        type = getIntent().getStringExtra("type");
        if (type.equals("end")) {
            settingTitle.setText("选择终点");
        } else {
            settingTitle.setText("选择起点");
        }
        bjTv.setVisibility(View.VISIBLE);
        bjTv.setText("首字母排序");
        bjTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("type", type);
                JumpUtils.jumptoNatvie(SubwayStartActivity.this, "SubwaySiteSorpListActivity", 0, bundle);
                finish();
            }
        });

        //选择起点和重点和上个页面一样  左边是szSubway.json 的内容
        //注意索引

        String subwayjson = CommonUtils.getJson(this, "subway.json");
        subwayLineBean = new Gson().fromJson(subwayjson, SubwayLineBean.class);


        String json = CommonUtils.getJson(this, "szSubway.json");
        Gson gson = new Gson();
        subwayLine = gson.fromJson(json, SubwayBean.class).getSubway_list();
        currentLine = 3;

        subwaySelectLineAdapter = new SubwaySelectLineAdapter( this, subwayLine);
        subwayLineLv.setLayoutManager(new LinearLayoutManager(this));
        subwayLineLv.setAdapter(subwaySelectLineAdapter);

        subwaySelectLineAdapter.setiOnClickListener(position -> {

            switch (position){
                case 0:
                    currentLine = 0;
                    break;
                case 1:
                    currentLine = 1;
                    break;
                case 2:
                    currentLine = 2;
                    break;
                case 3:
                    currentLine = 3;
                    break;
                case 4:
                    currentLine = 4;
                    break;

            }
            data = subwayLineBean.getLine_list().get(currentLine).getG();
            loadLine(currentLine);

        });

        sitelistAdapter = new SubwaySitelistAdapter(this);
        subwaySiteLv.setLayoutManager(new LinearLayoutManager(this));
        subwaySiteLv.setAdapter(sitelistAdapter);
        sitelistAdapter.setiSubwaySiteListClickListener(busStation -> {
            MesageEventBus mesageEventBus = new MesageEventBus();
            if (type.equals("end")) {
                mesageEventBus.setEnd(busStation.getA());
            } else {
                mesageEventBus.setStart(busStation.getA());
            }
            mesageEventBus.setLat(busStation.getB().getLatitude());
            mesageEventBus.setLog(busStation.getB().getLongitude());
            EventBus.getDefault().postSticky(mesageEventBus);
            finish();
        });

        currentLine = 0;
        data = subwayLineBean.getLine_list().get(currentLine).getG();
        loadLine(currentLine);
    }

    @Override
    protected void updateViews() {

    }


    public void loadLine(int currentLine){
        int trueCurrentLine = currentLine==0?3:currentLine;
        sitelistAdapter.setBusStationList(subwayLineBean.getLine_list().get(currentLine).getG(),trueCurrentLine);
    }

}
